package com.gitee.neuray.security.wb.ribs.biz;

import com.gitee.neuray.security.common.biz.BaseBiz;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import com.gitee.neuray.security.wb.ribs.mapper.ApproveRoleMapper;
import com.gitee.neuray.security.wb.ribs.vo.ApproveRoleVo;
import com.gitee.neuray.security.wb.ribs.vo.RibsVo;
import com.gitee.neuray.security.wb.ribs.vo.SelectVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ApproveRoleBiz extends BaseBiz<ApproveRoleMapper, ApproveRoleVo> {

    @Autowired
    private ApproveRoleMapper approveRoleMapper;

    @Override
    protected String getPageName() {
        return null;
    }

    /**
     * 分页获取信息
     *
     * @param query,staffId
     * @return
     */
    public TableResultResponse page(Query query, String staffId, String type) {
        ApproveRoleVo approveRoleVo = new ApproveRoleVo();
        approveRoleVo.setStaffId(staffId);
        List<ApproveRoleVo> roleIds = new ArrayList<>();
        List<String> bxztStrs = new ArrayList<>();
        if(!"wechat".equals(type)){
            roleIds = super.selectList(approveRoleVo);
            if(roleIds != null && roleIds.size() != 0){

                for(ApproveRoleVo item:roleIds){
                    // 查询页面和审批页面各角色可见范围
                    // 如果是负责人，则只能看见自己部门的人员
                    String roleId = item.getRoleId();
                    String deptCode = item.getDeptCode();
                    if("1".equals(roleId)){
                        query.put("staffId",staffId);
                        if("approve".equals(type)){
                            query.put("querytype","approve");
                        }
                        query.put("rolebmfzr",true);
                    /*}else if("2".equals(roleId)){
                        if("approve".equals(type)){
                            bxztStrs.add("3");
                            query.put("querytype","approve");
                        }else if("query".equals(type)){
                            query.put("showall",true);
                            query.put("querytype","query");
                        }
                        query.put("rolezjl",true);*/
                    }else if("3".equals(roleId)){
                        if("approve".equals(type)){
                            bxztStrs.add("4");
                            query.put("querytype","approve");
                        }else if("query".equals(type)){
                            query.put("showall",true);
                            query.put("querytype","query");
                        }
                        query.put("rolekj",true);
                    }else if("4".equals(roleId)){
                        if("approve".equals(type)){
                            bxztStrs.add("6");
                            query.put("querytype","approve");
                        }else if("query".equals(type)){
                            query.put("showall",true);
                            query.put("querytype","query");
                        }
                        query.put("rolecwzj",true);
                    }
                }
                if(bxztStrs.size()!=0){
                    query.put("bxzts",bxztStrs);
                }
            }else{
                return new TableResultResponse(0, 0, 0, 0, new ArrayList());
            }
        }else {
            //微信设置查询人
            query.put("querytype","wechat");
            query.put("userId",staffId);
        }
        /* 审批页面各角色可见范围
        审批角色（ 1、部门审批负责人 2、总经理 3、会计 4、财务总监 ）
        报销状态（ 1、已保存 2、已提交 3、已初审 4、已复审 5、驳回 6、已复核 7、已打款 ）
        角色-状态
        1-2，2-3，3-4，3-6 */
        /*if("approve".equals(type)){
            if("1".equals(roleIds.get(0).getRoleId())){
                query.put("bxzt","2");
            }else if("2".equals(roleIds.get(0).getRoleId())){
                query.put("bxzt","3");
            }else if("3".equals(roleIds.get(0).getRoleId())){
                query.put("bxzt","4");
            }else if("4".equals(roleIds.get(0).getRoleId())){
                query.put("bxzt","6");
            }
        }*/
        //Query query = new Query(query);
        Page<Object> result = PageHelper.startPage(query.getPageNo(), query.getPageSize());

        List<RibsVo> list = approveRoleMapper.page(query);

        return new TableResultResponse(
                result.getPageSize(), result.getPageNum(), result.getPages(), result.getTotal(), list);
    }
    public List<RibsVo> exportQuery(Query query, String staffId) {
        ApproveRoleVo approveRoleVo = new ApproveRoleVo();
        approveRoleVo.setStaffId(staffId);
        List<ApproveRoleVo> roleIds = new ArrayList<>();
        roleIds = super.selectList(approveRoleVo);
        List<String> bxztStrs = new ArrayList<>();
        // 查询页面和审批页面各角色可见范围
        // 如果是负责人，则只能看见自己部门的人员
        if(roleIds!=null && roleIds.size()!=0){
            if("1".equals(roleIds.get(0).getRoleId())){
                query.put("staffId",staffId);
            }
        }
        roleIds = super.selectList(approveRoleVo);
        if(roleIds != null && roleIds.size() != 0) {

            for (ApproveRoleVo item : roleIds) {
                // 查询页面和审批页面各角色可见范围
                // 如果是负责人，则只能看见自己部门的人员
                String roleId = item.getRoleId();
                String deptCode = item.getDeptCode();
                if ("1".equals(roleId)) {
                    query.put("staffId", staffId);
                    query.put("querytype", "query");
                    query.put("rolebmfzr", true);
                } else if ("3".equals(roleId)) {
                    query.put("showall", true);
                    query.put("querytype", "query");
                    query.put("rolekj", true);
                } else if ("4".equals(roleId)) {
                    query.put("showall", true);
                    query.put("querytype", "query");
                    query.put("rolecwzj", true);
                }
            }
            if (bxztStrs.size() != 0) {
                query.put("bxzts", bxztStrs);
            }
        }
        /* 审批页面各角色可见范围
        审批角色（ 1、部门审批负责人 2、总经理 3、会计 4、财务总监 ）
        报销状态（ 1、已保存 2、已提交 3、已初审 4、已复审 5、驳回 6、已复核 7、已打款 ）
        角色-状态
        1-2，2-3，3-4，3-6 */
        /*if("1".equals(roleIds.get(0).getRoleId())){
            query.put("bxzt","2");
        }else if("2".equals(roleIds.get(0).getRoleId())){
            query.put("bxzt","3");
        }else if("3".equals(roleIds.get(0).getRoleId())){
            query.put("bxzt","4");
        }else if("4".equals(roleIds.get(0).getRoleId())){
            query.put("bxzt","6");
        }*/
        //Query query = new Query(query);

        List<RibsVo> list = approveRoleMapper.page(query);
        return list;
    }

    public List<RibsVo> exportApprove(Query query, String staffId) {
        ApproveRoleVo approveRoleVo = new ApproveRoleVo();
        approveRoleVo.setStaffId(staffId);
        List<ApproveRoleVo> roleIds = new ArrayList<>();
        List<String> bxztStrs = new ArrayList<>();
        roleIds = super.selectList(approveRoleVo);
        if (roleIds != null && roleIds.size() != 0) {

            for (ApproveRoleVo item : roleIds) {
                // 查询页面和审批页面各角色可见范围
                // 如果是负责人，则只能看见自己部门的人员
                String roleId = item.getRoleId();
                String deptCode = item.getDeptCode();
                if ("1".equals(roleId)) {
                    query.put("staffId", staffId);
                    query.put("querytype", "approve");
                    query.put("rolebmfzr", true);
                /*} else if ("2".equals(roleId)) {
                    bxztStrs.add("3");
                    query.put("querytype", "approve");
                    query.put("rolezjl", true);*/
                } else if ("3".equals(roleId)) {
                    bxztStrs.add("4");
                    query.put("querytype", "approve");
                    query.put("rolekj", true);
                } else if ("4".equals(roleId)) {
                    bxztStrs.add("6");
                    query.put("querytype", "approve");
                    query.put("rolecwzj", true);
                }
            }
            if (bxztStrs.size() != 0) {
                query.put("bxzts", bxztStrs);
            }
        }
        //Query query = new Query(query);

        List<RibsVo> list = approveRoleMapper.page(query);
        return list;
    }

    /**
     * 获取每种报销类型的待审批总条数
     * */
    public List<Integer> count(Map<String,Object> param, String staffId, String type){
        ApproveRoleVo approveRoleVo = new ApproveRoleVo();
        approveRoleVo.setStaffId(staffId);
        List<ApproveRoleVo> roleIds = new ArrayList<>();
        if("approve".equals(type)){
            roleIds = super.selectList(approveRoleVo);
            // 查询页面和审批页面各角色可见范围
            // 如果是负责人，则只能看见自己部门的人员
            if(roleIds!=null && roleIds.size()!=0){
                for(ApproveRoleVo vo:roleIds){
                    if("1".equals(vo.getRoleId())){
                        param.put("staffId",staffId);
                        param.put("roleBmfzr",true);
                    /*}else if("2".equals(vo.getRoleId())){
                        param.put("roleZjl",true);*/
                    }else if("3".equals(vo.getRoleId())){
                        param.put("roleKj",true);
                    }else if("4".equals(vo.getRoleId())){
                        param.put("roleCwzj",true);
                    }

                }
            }
        }
        /* 审批页面各角色可见范围
        审批角色（ 1、部门审批负责人 2、总经理 3、会计 4、财务总监 ）
        报销状态（ 1、已保存 2、已提交 3、已初审 4、已复审 5、驳回 6、已复核 7、已打款 ）
        角色-状态
        1-2，2-3，3-4，3-6 */
//        if("approve".equals(type)){
//            if("1".equals(roleIds.get(0).getRoleId())){
//                param.put("bxzt","2");
//            }else if("2".equals(roleIds.get(0).getRoleId())){
//                param.put("bxzt","3");
//            }else if("3".equals(roleIds.get(0).getRoleId())){
//                param.put("bxzt","4");
//            }else if("4".equals(roleIds.get(0).getRoleId())){
//                param.put("bxzt","6");
//            }
//
//        }
        String[] arr = { "1","2","3","5"};
        List<Integer> list = new ArrayList<>();
        for(String str:arr){
            param.put("bxlx",str);
            list.add(approveRoleMapper.count(param));
        }

        return list;
    }

    public List<SelectVo> getXS(String deptName){
        return approveRoleMapper.getXS(deptName);
    }
}
